{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "a4da881e",
      "metadata": {},
      "source": [
        "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pinecone-io/examples/blob/master/learn/generation/langchain/rag-sparse-query-expansion.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/pinecone-io/examples/blob/master/learn/generation/langchain/rag-sparse-query-expansion.ipynb)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "9abb5eee-b493-42f2-84c4-2615286cfeb5",
      "metadata": {
        "id": "9abb5eee-b493-42f2-84c4-2615286cfeb5"
      },
      "source": [
        "# Leveraging Sparse Embeddings for Financial Reddit Analysis\n",
        "\n",
        "In this example, we'll build an agent that can search through Reddit posts about stocks and financial markets\n",
        "using sparse embeddings. Unlike dense embeddings that capture overall semantic meaning, sparse embeddings excel at\n",
        "capturing specific keywords and rare terms - perfect for financial contexts where stock tickers (like AAPL, TSLA)\n",
        "and company names need to be precisely matched.\n",
        "\n",
        "## Why Sparse Embeddings for Financial Data?\n",
        "\n",
        "Financial discussions on platforms like Reddit often contain:\n",
        "1. Stock tickers (PLTR, NVDA) that need exact matching\n",
        "2. Company names that might be misspelled (Palanteer vs. Palantir)\n",
        "3. Industry-specific jargon that dense embeddings might miss\n",
        "\n",
        "Sparse embeddings shine here because they:\n",
        "- Maintain high precision for exact term matches (crucial for tickers)\n",
        "- Preserve term frequency information (how often a stock is mentioned)\n",
        "- Allow for better retrieval of documents with rare but important terms\n",
        "\n",
        "Let's see how we can implement this approach with Pinecone's sparse vector database."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "92b2fffc",
      "metadata": {},
      "source": [
        "### Before you begin\n",
        "\n",
        "You'll need to get an [OpenAI API key](https://platform.openai.com/account/api-keys) and [Pinecone API key](https://app.pinecone.io)."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "R6GUuNTJZK99",
      "metadata": {
        "id": "R6GUuNTJZK99"
      },
      "source": [
        "First, we install the prerequisites:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "HVd0kPOmZKZq",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "HVd0kPOmZKZq",
        "outputId": "ea8ed8b2-ab87-47f1-d2c0-df92ef169e8b"
      },
      "outputs": [],
      "source": [
        "!pip install -qU \\\n",
        "  langchain==0.3.25 \\\n",
        "  langchain-community==0.3.24 \\\n",
        "  langchain-pinecone==0.2.6 \\\n",
        "  langchain-openai==0.3.18 \\\n",
        "  datasets==3.6.0"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "m3CCZqZkZFjP",
      "metadata": {
        "id": "m3CCZqZkZFjP"
      },
      "source": [
        "## Data Prep"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "yvaBFRp1ZSnX",
      "metadata": {
        "id": "yvaBFRp1ZSnX"
      },
      "source": [
        "We will use a dataset of posts from various finances subreddits, like `r/stocks`, `r/finance`, `r/pennystocks`, and `r/wallstreetbets`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "3xFpVByF64SG",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 307,
          "referenced_widgets": [
            "aebf1e40a991483ab370357ea5fb25da",
            "6f785a0107734536ae2d563999e06380",
            "67297bee0e044e0ba1c8646960cb1838",
            "79a9f841bb3e43d7b54a842707096df0",
            "a310a78db6784da883b25e298929ce53",
            "75ab4770ea3148cea8c5782c637c5332",
            "ecf822ec0db24cfc9e86f45800f06c06",
            "774baf7fc7d6477386e423f1a40fb78f",
            "9ec9896e6a7e4f38847854b7a5ff466a",
            "c95c7806467b4084ac98d56ce5432452",
            "66c12db017294950b2a8c388e9916e30",
            "f8f33f59739b45c09dc43e5281424edc",
            "7b8397aa872a434ca86a01e7d6ae5ae5",
            "f11607fe14b84ee0af30aea511601a1f",
            "9287a8c92efc432c9fa0796d754b37cd",
            "d189393070244726b2d8b4208e494173",
            "992507f24ed5412b99562b25a93bd75e",
            "acc5c7bcfcf34783bf49fc20f3799817",
            "d19bb7fd3f524afc8e1977c5b0f76f20",
            "d1aa0790520f41d1808b285ed8841692",
            "bb916a31ca8c47b5aced37da88e0bd9e",
            "4a03e48b425440aab1b7ae18100e4fe8",
            "c5193af9e5d5438caed44b981026384b",
            "b3ceda88931f45a783cc7645e76aa332",
            "850a3e7d839c4374aea154b2226c3da1",
            "70ab5ead3c1e4852a478bb98206e27aa",
            "0458b728499448008c51752f942786e4",
            "680d47ffb95741e3abe5260dc7d8e7b5",
            "1f34a1d2a9c24467a1a70fa8c75e9edb",
            "a0b3328199e842eebdd9714450c04f03",
            "78ce3cbd53754d51b45fe73aed560313",
            "2fa599a5abce4d38b9e5009d639229d9",
            "c1bbb693417845c69fbade194d473a91"
          ]
        },
        "id": "3xFpVByF64SG",
        "outputId": "164fb83a-b7cb-46b1-dd6e-833d3c9c12ea"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n",
            "The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
            "To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
            "You will be able to reuse this secret in all of your notebooks.\n",
            "Please note that authentication is recommended but still optional to access public models or datasets.\n",
            "  warnings.warn(\n"
          ]
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "aebf1e40a991483ab370357ea5fb25da",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "README.md:   0%|          | 0.00/24.0 [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "f8f33f59739b45c09dc43e5281424edc",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "train.jsonl:   0%|          | 0.00/150k [00:00<?, ?B/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "c5193af9e5d5438caed44b981026384b",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Generating train split:   0%|          | 0/107 [00:00<?, ? examples/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/plain": [
              "Dataset({\n",
              "    features: ['id', 'subreddit', 'title', 'selftext'],\n",
              "    num_rows: 107\n",
              "})"
            ]
          },
          "execution_count": 2,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from datasets import load_dataset\n",
        "\n",
        "posts = load_dataset(\"aurelio-ai/reddit-finance\", split=\"train\")\n",
        "posts"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "P5xPYZ9h_CqH",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "P5xPYZ9h_CqH",
        "outputId": "87992133-cc20-4505-f860-20281857ed9f"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{'id': '1j0w73o',\n",
              " 'subreddit': 'stocks',\n",
              " 'title': 'Rate My Portfolio - r/Stocks Quarterly Thread March 2025',\n",
              " 'selftext': \"Please use this thread to discuss your portfolio, learn of other stock tickers &amp; portfolios like [Warren Buffet's](https://buffett.online/en/portfolio/), and help out users by giving constructive criticism.\\n\\nWhy quarterly?  Public companies report earnings quarterly; many investors take this as an opportunity to rebalance their portfolios.  We highly recommend you do some reading:  Check out our wiki's list of [relevant posts &amp; book recommendations.](https://www.reddit.com/r/stocks/wiki/index/#wiki_relevant_posts.2C_books.2C_wiki_recommendations)\\n\\nYou can find stocks on your own by using a scanner like your broker's or [Finviz.](https://finviz.com/screener.ashx)  To help further, here's a list of [relevant websites.](https://www.reddit.com/r/stocks/wiki/index/#wiki_relevant_websites.2Fapps)\\n\\nIf you don't have a broker yet, see our [list of brokers](https://www.reddit.com/r/stocks/wiki/index/#wiki_brokers_for_investing) or search old posts.  If you haven't started investing or trading yet, then setup your [paper trading to learn basics like market orders vs limit orders.](https://www.reddit.com/r/stocks/wiki/index/#wiki_is_there_a_way_to_practice.3F)\\n\\nBe aware of [Business Cycle Investing](https://eresearch.fidelity.com/eresearch/markets_sectors/sectors/si_business_cycle.jhtml?tab=sibusiness) which Fidelity issues updates to the state of global business cycles every 1 to 3 months (note: Fidelity changes their links often, so search for it since their take on it is enlightening).  [Investopedia's take on the Business Cycle](https://www.investopedia.com/articles/investing/061316/business-cycle-investing-ratios-use-each-cycle.asp).\\n\\nIf you need help with a falling stock price, check out Investopedia's [The Art of Selling A Losing Position](https://www.investopedia.com/articles/02/022002.asp) and their [list of biases.](https://www.investopedia.com/articles/stocks/08/capital-losses.asp)\\n\\nHere's a list of all the [previous portfolio stickies.](https://www.reddit.com/r/stocks/search?q=author%3Aautomoderator+title%3A%22Rate+My+Portfolio%22&amp;restrict_sr=on&amp;sort=new&amp;t=all)\"}"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "posts[0]"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "dqhwvp_dC2kD",
      "metadata": {
        "id": "dqhwvp_dC2kD"
      },
      "source": [
        "We add the post links like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "id": "YIlyiC_XC4zP",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 49,
          "referenced_widgets": [
            "d9ef0f4f9bea4566b991162d9a0a192a",
            "8f59bb90d7f34432b5c2fc2bc6dd1e55",
            "a7bb2a042df24ee8aa4b74998a911a77",
            "ec044501ca6e4ebb8b50ec826194de0b",
            "4d81dbed2e7a43f2a20464fb617e052d",
            "e264689722ab4978b99b65108a26aadd",
            "a2252d52ac4348ef8ca2cb15cffc5599",
            "f0826b72141046bc880a6eb872915702",
            "9949b847a35346249bcbe9f37c00b8fa",
            "fc6e0d801ec145b8a389abce24f8c354",
            "873234c99e874c4b8a7a129da1b86ec0"
          ]
        },
        "id": "YIlyiC_XC4zP",
        "outputId": "56853541-8112-444c-c236-72e7f5014394"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "d9ef0f4f9bea4566b991162d9a0a192a",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "Map:   0%|          | 0/107 [00:00<?, ? examples/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "def add_permalink(x):\n",
        "    x[\"permalink\"] = f\"reddit.com/r/{x['subreddit']}/comments/{x['id']}\"\n",
        "    return x\n",
        "\n",
        "posts = posts.map(add_permalink)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "1ebf0466-0349-4d54-bbcc-c4b820680b16",
      "metadata": {
        "id": "1ebf0466-0349-4d54-bbcc-c4b820680b16"
      },
      "source": [
        "## Sparse Vector Index Setup\n",
        "\n",
        "For our Reddit stocks analysis, we need a specialized vector index that supports sparse vectors.\n",
        "Unlike dense vectors (which might be 1024 dimensions with most values being non-zero),\n",
        "sparse vectors can have millions of dimensions but only a few non-zero values.\n",
        "\n",
        "This sparse representation is perfect for capturing the presence of specific terms like \"NVDA\" or \"earnings call\"\n",
        "without diluting their importance in a dense representation.\n",
        "\n",
        "## Sparse Embeddings Model\n",
        "\n",
        "We'll use Pinecone's specialized sparse embedding model that's designed to:\n",
        "1. Identify important financial terms and stock tickers\n",
        "2. Create sparse vector representations that emphasize these key terms\n",
        "3. Enable high-precision matching for financial queries"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "id": "6d16992c-c709-468d-8bcb-fcb8ba82a76a",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "6d16992c-c709-468d-8bcb-fcb8ba82a76a",
        "outputId": "ae32db74-a0ec-4218-ea16-c24bfb963f39"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Enter your Pinecone API key: ··········\n"
          ]
        }
      ],
      "source": [
        "import os\n",
        "from getpass import getpass\n",
        "from pinecone import Pinecone\n",
        "\n",
        "os.environ[\"PINECONE_API_KEY\"] = os.getenv(\"PINECONE_API_KEY\") \\\n",
        "    or getpass(\"Enter your Pinecone API key: \")\n",
        "\n",
        "pc = Pinecone()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "id": "UGproNmdjUV0",
      "metadata": {
        "id": "UGproNmdjUV0"
      },
      "outputs": [],
      "source": [
        "from pinecone import ServerlessSpec, CloudProvider, AwsRegion, Metric\n",
        "\n",
        "index_name = \"rag-sparse-query-expansion\"\n",
        "\n",
        "if not pc.has_index(name=index_name):\n",
        "    pc.create_index(\n",
        "        name=index_name,\n",
        "        metric=Metric.DOTPRODUCT,\n",
        "        dimension=None,\n",
        "        spec=ServerlessSpec(\n",
        "            cloud=CloudProvider.AWS,\n",
        "            region=AwsRegion.US_EAST_1\n",
        "        ),\n",
        "        vector_type=\"sparse\",\n",
        "    )\n",
        "\n",
        "index = pc.Index(name=index_name)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "umjIfc9ojnxP",
      "metadata": {
        "id": "umjIfc9ojnxP"
      },
      "source": [
        "Now we initialize our sparse embedding model:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "Ru1-rcPUb0Qe",
      "metadata": {
        "id": "Ru1-rcPUb0Qe"
      },
      "outputs": [],
      "source": [
        "from langchain_pinecone.embeddings import PineconeSparseEmbeddings\n",
        "\n",
        "sparse_embeddings = PineconeSparseEmbeddings(\n",
        "    model=\"pinecone-sparse-english-v0\", show_progress_bar=True\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "PIyAuk7-juIC",
      "metadata": {
        "id": "PIyAuk7-juIC"
      },
      "source": [
        "We bring these together to initialize our LangChain `PineconeSparseVectorStore` object."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "RvkFu8TTj0vg",
      "metadata": {
        "id": "RvkFu8TTj0vg"
      },
      "outputs": [],
      "source": [
        "from langchain_pinecone import PineconeSparseVectorStore\n",
        "\n",
        "vector_store = PineconeSparseVectorStore(\n",
        "    index=index, embedding=sparse_embeddings\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "AcxARty_j9ss",
      "metadata": {
        "id": "AcxARty_j9ss"
      },
      "source": [
        "Now we index everything. We will be embedding both the post title and text content, this helps us capture greater context than just embedding one or the other."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "fa3c17f7-f1f9-4ddb-a14a-af7cf2c4e8a8",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 113,
          "referenced_widgets": [
            "770a72421b444c51a3c9ffb000575608",
            "f30e92014f654f4a8542b842036d4511",
            "c8104609fc4d42c3b3479ffd7261a069",
            "41cee4afec014df698f960d347ff6647",
            "119d9b43f0084c5d8c80d49844978903",
            "67e4337bb3664e4bb2c68613e49a9650",
            "c34b76c784d44b139ec822c0ae0f8cfb",
            "fccd72ecd1644e90bedecbdbc9cc03b3",
            "35f905e43805458692f010db5d05c09b",
            "9f26e31decc14ceaa0dc015360a010a4",
            "9650aec1359c4c1ba2d8524231e51a6a",
            "bad6b887541b4a4494646aad3dfcb8bd",
            "be8ca077ad9640b5bc76929183036ea4",
            "d35f4c27acab467faaa44306f13f0e16",
            "c88b54682b834927be7d3ad323b1b635",
            "9b8b5d4b7f314b80b32185f15e48be03",
            "6562254fed0448418d24c69ecb302d83",
            "7c4008623b644fcd9f8e49d92ae5dabe",
            "2d1ffb42cdb14e7cbda1e382f978b8b0",
            "74294e31624d4e4593986f2ff9a27701",
            "033a31c7a9ee45688f6621a9b8f0f33c",
            "07ab4fd787f949d697f69648f11f9299",
            "b37a571d0b94448ca077fecc7c77ad45",
            "6d5f9a3346d84328a2dfb8dd6d424ae9",
            "06e749050a9e45ada8c033665ac0266a",
            "d2e14df35e0e4e5fa90f9d7be4273055",
            "4fe8d7818a20485c9c2c6caf38275bc0",
            "8765aebe728c4e00b2b7fb2b91f36499",
            "b9c6944dbfe34a4892c1fd2387340f23",
            "6a6332eb0791445b9244276b772ca0b2",
            "94d6ba2764274fb39c96b63102db8956",
            "79ac8374e30440a29a24c474c9abe12e",
            "fca495705dce440d9bab9b637ee030a4"
          ]
        },
        "id": "fa3c17f7-f1f9-4ddb-a14a-af7cf2c4e8a8",
        "outputId": "f3eb15ed-dc5c-4bcc-de71-22573d88ff06"
      },
      "outputs": [
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "770a72421b444c51a3c9ffb000575608",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "  0%|          | 0/2 [00:00<?, ?it/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "bad6b887541b4a4494646aad3dfcb8bd",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "  0%|          | 0/2 [00:00<?, ?it/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "application/vnd.jupyter.widget-view+json": {
              "model_id": "b37a571d0b94448ca077fecc7c77ad45",
              "version_major": 2,
              "version_minor": 0
            },
            "text/plain": [
              "  0%|          | 0/1 [00:00<?, ?it/s]"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "from tqdm.auto import tqdm\n",
        "\n",
        "# we add records in batches of 100 - for large datasets this is essential to\n",
        "# avoid excessively large requests or out-of-memory errors\n",
        "batch_size = 100\n",
        "\n",
        "for i in tqdm(range(0, len(posts), batch_size)):\n",
        "    # grab batch\n",
        "    end_idx = min(i + batch_size, len(posts))\n",
        "    batch = posts.select(range(i, end_idx))\n",
        "    # merge title and content before embedding\n",
        "    title_and_content = [\n",
        "        f\"## {post['title']}\\n\\n{post['selftext']}\" for post in batch\n",
        "    ]\n",
        "    # prep our metadata\n",
        "    metadata_batch = [\n",
        "        {\n",
        "            \"subreddit\": post[\"subreddit\"],\n",
        "            \"permalink\": post[\"permalink\"]\n",
        "        } for post in batch\n",
        "    ]\n",
        "    # add our data to the vec store\n",
        "    vector_store.add_texts(\n",
        "        texts=title_and_content,\n",
        "        metadatas=metadata_batch[i:end_idx]\n",
        "    )"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "efa317a4-bf9e-45c1-bcd4-3384838a7437",
      "metadata": {
        "id": "efa317a4-bf9e-45c1-bcd4-3384838a7437"
      },
      "source": [
        "## Query Expansion: Bridging ticker and company names for financial search\n",
        "\n",
        "One of the biggest challenges in financial search is the vocabulary mismatch problem:\n",
        "- Users might search for \"Palanteer\" when they mean \"Palantir\" (PLTR)\n",
        "- They might use \"NVDA stock drop\" when relevant posts mention \"Nvidia shares plummeting\"\n",
        "- They might search for a company name when Reddit posts use only the ticker symbol\n",
        "\n",
        "To solve this, we'll implement query expansion - a technique that transforms a single query\n",
        "into multiple alternative queries that capture the same intent but with different terminology.\n",
        "\n",
        "We'll be using OpenAI for this part, for which we need an [API key](https://platform.openai.com) which we enter below:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "id": "kyNhQKn2j-Si",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "kyNhQKn2j-Si",
        "outputId": "b23acdc7-7633-4c81-9d0f-da81ed6806ec"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Enter your OpenAI API key: ··········\n"
          ]
        }
      ],
      "source": [
        "os.environ[\"OPENAI_API_KEY\"] = os.getenv(\"OPENAI_API_KEY\") \\\n",
        "    or getpass(\"Enter your OpenAI API key: \")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "NbcYqQb3mWm9",
      "metadata": {
        "id": "NbcYqQb3mWm9"
      },
      "source": [
        "Now we define our LLM that will be used _within_ our query expansion tool to create our expanded queries. We can use `with_structured_output` to force a particular output format."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "id": "Kj5QbI4qmnDF",
      "metadata": {
        "id": "Kj5QbI4qmnDF"
      },
      "outputs": [],
      "source": [
        "from pydantic import BaseModel\n",
        "from langchain_openai import ChatOpenAI\n",
        "\n",
        "# the pydantic BaseModel defines the schema we want for our LLM output\n",
        "class QueryArray(BaseModel):\n",
        "    queries: list[str]\n",
        "\n",
        "# LLM for query expansion with high temp for increased \"creativity\"\n",
        "# and structured output\n",
        "expansion_llm = ChatOpenAI(\n",
        "    model=\"gpt-4.1-mini\", temperature=0.7\n",
        ").with_structured_output(QueryArray)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "fcD7uNAsm_LS",
      "metadata": {
        "id": "fcD7uNAsm_LS"
      },
      "source": [
        "Next we define the prompt that explains to our `expansion_llm` what it's purpose is and how it should tackle it's query expansion task."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 14,
      "id": "edbb98e2-e58e-49a3-a995-4570e1733e52",
      "metadata": {
        "id": "edbb98e2-e58e-49a3-a995-4570e1733e52"
      },
      "outputs": [],
      "source": [
        "expand_query_prompt = (\n",
        "    \"You are an expert at expanding search queries to improve search \"\n",
        "    \"results. Given a query about stocks, companies, or financial \"\n",
        "    \"markets, generate {num_expansions} alternative queries that \"\n",
        "    \"capture the same intent but use different wording, synonyms, or \"\n",
        "    \"related concepts.\\n\"\n",
        "    \"For company names, include both the company name and ticker \"\n",
        "    \"symbol when possible. For misspelled company names, include the \"\n",
        "    \"correct spelling in the alternatives. For general topics, i.e. \"\n",
        "    '\"tech company performance\", expand these queries to specific '\n",
        "    'companies you know of, like \"Google\", \"Apple\", \"Nvidia\".\\n'\n",
        "    'Here are some examples:\\n\"For example, a query about Google could expand to: [\"GOOGL\", '\n",
        "    '\"FAANG company performance\" should expand to: \"'\n",
        "    '[\"META performance\", \"GOOGL performance\", \"AAPL performance\"]\\n'\n",
        "    '\"Tesla stock drop\" should expand to: '\n",
        "    '[\"TSLA share price decrease\", \"Tesla Motors stock market decline\",'\n",
        "    ' \"EV companies market decline\", \"BYD stock performance\",'\n",
        "    ' \"Elon Musk company stock performance\"]\\n'\n",
        "    \"The goal is to get a broader yet relevant scope of results.\\n\"\n",
        "    \"Original query: {query}\"\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "8psXxSmNnZtL",
      "metadata": {
        "id": "8psXxSmNnZtL"
      },
      "source": [
        "We can run the LLM directly like so:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "id": "fKcygmwEjn1k",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "fKcygmwEjn1k",
        "outputId": "b1786cc5-6e33-4ad1-dc11-2c46511c318c"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "QueryArray(queries=['NVIDIA stock price', 'NVDA share performance', 'NVIDIA Corporation stock market', 'Graphics card company stock NVDA', 'Semiconductor stocks including NVIDIA'])"
            ]
          },
          "execution_count": 15,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# get response from LLM\n",
        "response = expansion_llm.invoke(expand_query_prompt.format(\n",
        "    query=\"nvidia stonks\",\n",
        "    num_expansions=5\n",
        "))\n",
        "response"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "pRtdc3u-nJMy",
      "metadata": {
        "id": "pRtdc3u-nJMy"
      },
      "source": [
        "However, we'd like this query expansion functionality to be an optional _tool_ that our main agent can call. For this, we define the `expand_query` tool. The main agent will decide whether to use this tool (or not) based on the instructions we provide in the function docstring below:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "id": "1kio5FvWnJga",
      "metadata": {
        "id": "1kio5FvWnJga"
      },
      "outputs": [],
      "source": [
        "from langchain_core.tools import tool\n",
        "\n",
        "@tool\n",
        "def expand_query(query: str, num_expansions: int = 5) -> list[str]:\n",
        "    \"\"\"\n",
        "    Expand a search query into multiple alternative queries to improve search\n",
        "    results. Ideally you must capture the broader scope of queries beyond the\n",
        "    provided query.\n",
        "    This is useful when:\n",
        "    - The original query is vague or ambiguous\n",
        "    - You need to find synonyms or related terms\n",
        "    - You want to cover broader aspects of the topic\n",
        "    - The query contains misspelled company names or stock tickers\n",
        "\n",
        "    Args:\n",
        "        query: The original query that needs to be expanded\n",
        "        num_expansions: Number of alternative queries to generate\n",
        "\n",
        "    Returns:\n",
        "        A list of expanded queries including the original query\n",
        "    \"\"\"\n",
        "    try:\n",
        "        # get response from LLM\n",
        "        response = expansion_llm.invoke(expand_query_prompt.format(\n",
        "            query=query,\n",
        "            num_expansions=num_expansions\n",
        "        ))\n",
        "        # return the expanded queries\n",
        "        return response.queries\n",
        "    except Exception as e:\n",
        "        return [f\"Error expanding query: {str(e)}\"]"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "16277004-c1a1-4916-a396-cf4237e88fbc",
      "metadata": {
        "id": "16277004-c1a1-4916-a396-cf4237e88fbc"
      },
      "source": [
        "## Searching Reddit with Sparse Vectors\n",
        "\n",
        "Now that we have our query expansion mechanism, let's create a tool that leverages both\n",
        "sparse embeddings and query expansion to search through Reddit posts about stocks.\n",
        "\n",
        "The magic happens in the combination:\n",
        "1. Query expansion generates multiple versions of the search query\n",
        "2. Each expanded query is converted to a sparse vector representation\n",
        "3. Pinecone's sparse similarity search finds the most relevant posts\n",
        "4. Results are deduplicated and ranked by relevance"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "id": "883d6054-144a-499d-b56d-716bc4803e2a",
      "metadata": {
        "id": "883d6054-144a-499d-b56d-716bc4803e2a"
      },
      "outputs": [],
      "source": [
        "from typing import Any\n",
        "from langchain_core.documents import Document\n",
        "\n",
        "@tool\n",
        "def reddit_stock_mentions(\n",
        "    queries: list[str], max_results: int = 10, **kwargs\n",
        ") -> list[dict[str, Any]]:\n",
        "    \"\"\"\n",
        "    Search through Reddit posts from finance subreddits like r/wallstreetbets,\n",
        "    r/investing, and r/pennystocks to find mentions of stocks, companies, or\n",
        "    market trends.\n",
        "\n",
        "    When using this tool:\n",
        "    - For company searches, include both the company name and ticker symbol if\n",
        "      known (e.g., \"Tesla TSLA\")\n",
        "    - For misspelled company names, provide the correct spelling\n",
        "    - Consider alternative terms that might be used on Reddit (e.g., \"$PLTR\"\n",
        "      instead of just \"Palantir\")\n",
        "    - Be specific about the type of information you're looking for (e.g.,\n",
        "      \"NVIDIA earnings reaction\")\n",
        "\n",
        "    Args:\n",
        "        query: search query to find relevant posts about stocks or companies\n",
        "        max_results: how many posts to return\n",
        "\n",
        "    Returns:\n",
        "        A list of relevant Reddit posts with metadata\n",
        "    \"\"\"\n",
        "    try:\n",
        "        seen_docs = set()  # To track unique documents\n",
        "        docs: list[tuple[Document, float]] = []\n",
        "\n",
        "        # Perform the search using sparse vectors\n",
        "        # This is where the magic happens - sparse vectors excel at matching\n",
        "        # specific terms like stock tickers and company names\n",
        "        for query in queries:\n",
        "            docs.extend(\n",
        "                vector_store.similarity_search_with_score(\n",
        "                    query=query, k=max_results\n",
        "                )\n",
        "            )\n",
        "\n",
        "        unique_docs: list[dict] = []\n",
        "\n",
        "        # Add unique results\n",
        "        for doc, score in docs:\n",
        "            # Create a unique identifier for the document\n",
        "            doc_id = f\"{doc.metadata.get('permalink', '')}\"\n",
        "\n",
        "            # Only add if we haven't seen this document before\n",
        "            if doc_id not in seen_docs:\n",
        "                seen_docs.add(doc_id)\n",
        "                unique_docs.append({\n",
        "                    \"content\": doc.page_content,\n",
        "                    \"subreddit\": doc.metadata.get(\"subreddit\", \"unknown\"),\n",
        "                    \"score\": score,\n",
        "                    \"url\": doc.metadata.get(\"permalink\", \"\"),\n",
        "                    \"matched_query\": query,\n",
        "                })\n",
        "\n",
        "        # Sort by score (if available) and limit to max_results\n",
        "        unique_docs.sort(key=lambda x: x.get(\"score\", 0), reverse=True)\n",
        "        return unique_docs[:max_results]\n",
        "\n",
        "    except Exception as e:\n",
        "        return [{\"error\": f\"Error searching Reddit mentions: {str(e)}\"}]\n"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "dad7bcd9-be7a-4896-81c1-17193d273073",
      "metadata": {
        "id": "dad7bcd9-be7a-4896-81c1-17193d273073"
      },
      "source": [
        "## Putting It All Together: The Reddit Stocks Agent\n",
        "\n",
        "Now we'll create an agent that orchestrates our tools to help users find and analyze\n",
        "what Reddit is saying about stocks and companies. The agent will:\n",
        "\n",
        "1. Use query expansion to handle misspellings and terminology variations\n",
        "2. Leverage sparse embeddings for precise matching of financial terms\n",
        "3. Analyze and summarize the Reddit sentiment about stocks\n",
        "\n",
        "This approach combines the best of both worlds:\n",
        "- LLMs for understanding user intent and generating alternative queries\n",
        "- Sparse vector search for high-precision retrieval of financial discussions\n",
        "\n",
        "We start with setting up our system prompt and chat prompt template, which will include placeholders for chat history _and_ intermediate steps where the agent will be able to store information returned from it's various tools before responding to the user."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "id": "1d6a9b44-087f-4d57-a215-cd4d8b79e62b",
      "metadata": {
        "id": "1d6a9b44-087f-4d57-a215-cd4d8b79e62b"
      },
      "outputs": [],
      "source": [
        "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
        "\n",
        "# Define a system prompt that emphasizes using query expansion\n",
        "system_prompt = (\n",
        "    \"You are a financial research assistant that helps users find information \"\n",
        "    \"about stocks and companies from Reddit discussions.\\n\\n\"\n",
        "    \"ALWAYS use the expand_query tool first when searching for information. \"\n",
        "    \"This is critical because:\\n\"\n",
        "    \"1. It helps find alt terms and synonyms that Reddit users might use\\n\"\n",
        "    \"2. It corrects misspelled company names or stock tickers\\n\"\n",
        "    \"3. It improves search coverage by generating multiple related queries\\n\\n\"\n",
        "    \"After expanding the query, use the reddit_stock_mentions tool with the \"\n",
        "    \"expanded queries to find relevant posts.\\n\"\n",
        "    \"Remember to analyze the results and provide a comprehensive summary of \"\n",
        "    \"what Reddit users are saying about the stock or company in question.\"\n",
        ")\n",
        "\n",
        "# Create the prompt template with message history\n",
        "prompt = ChatPromptTemplate.from_messages([\n",
        "    (\"system\", system_prompt),\n",
        "    MessagesPlaceholder(variable_name=\"chat_history\"),\n",
        "    (\"human\", \"{input}\"),\n",
        "    MessagesPlaceholder(variable_name=\"agent_scratchpad\")\n",
        "])"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "nByN7tPrEqvd",
      "metadata": {
        "id": "nByN7tPrEqvd"
      },
      "source": [
        "Now we setup chat memory, we'll use a simple conversation buffer (ie save everything)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "id": "4BQM_8mPErAk",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4BQM_8mPErAk",
        "outputId": "011c6743-d64c-43fc-dc76-139208a62d8e"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "<ipython-input-19-f8bdf340cea2>:3: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
            "  memory = ConversationBufferMemory(\n"
          ]
        }
      ],
      "source": [
        "from langchain.memory import ConversationBufferMemory\n",
        "\n",
        "memory = ConversationBufferMemory(\n",
        "    memory_key=\"chat_history\",\n",
        "    return_messages=True\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "BZPMNIaXE4V2",
      "metadata": {
        "id": "BZPMNIaXE4V2"
      },
      "source": [
        "Now bring all of this together to create our agent:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 24,
      "id": "M9j50dH2E4qd",
      "metadata": {
        "id": "M9j50dH2E4qd"
      },
      "outputs": [],
      "source": [
        "from langchain.agents import create_openai_tools_agent, AgentExecutor\n",
        "\n",
        "main_llm = ChatOpenAI(model=\"gpt-4.1\", temperature=0.0)\n",
        "tools = [expand_query, reddit_stock_mentions]\n",
        "\n",
        "agent = create_openai_tools_agent(\n",
        "    llm=main_llm, tools=tools, prompt=prompt\n",
        ")\n",
        "\n",
        "# package into an agent executor\n",
        "agent_executor = AgentExecutor(\n",
        "    agent=agent,\n",
        "    tools=tools,\n",
        "    memory=memory,\n",
        "    verbose=True,\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "4828013a-cfd7-47c1-b1fb-8820f7f54947",
      "metadata": {
        "id": "4828013a-cfd7-47c1-b1fb-8820f7f54947"
      },
      "source": [
        "## Testing Our Agent with Real-World Queries\n",
        "\n",
        "Let's put our agent to the test with some realistic queries that demonstrate its capabilities:\n",
        "1. Handling misspelled company names (Palanteer → Palantir)\n",
        "2. Finding discussions about specific events (NVIDIA earnings)\n",
        "3. Exploring broader topics (biotech companies)\n",
        "\n",
        "The agent will:\n",
        "- Expand each query to cover different terminology\n",
        "- Search using sparse vectors for precise matching\n",
        "- Analyze and summarize the Reddit sentiment"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 25,
      "id": "e01483f6-a92c-446f-bd8a-7048d8a5bf82",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 882
        },
        "id": "e01483f6-a92c-446f-bd8a-7048d8a5bf82",
        "outputId": "1b966053-b1be-4382-f827-30dc591e0495"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
            "\u001b[32;1m\u001b[1;3m\n",
            "Invoking: `expand_query` with `{'query': 'semiconductor companies stock recent news', 'num_expansions': 6}`\n",
            "\n",
            "\n",
            "\u001b[0m\u001b[36;1m\u001b[1;3m['semiconductor industry stock market updates', \"latest news on chip manufacturers' shares\", 'recent developments in semiconductor company stocks', 'stock performance news for companies like NVIDIA and AMD', 'market trends in semiconductor stocks such as Intel and Qualcomm', \"updates on semiconductor firms' share prices and financial news\"]\u001b[0m\u001b[32;1m\u001b[1;3m\n",
            "Invoking: `reddit_stock_mentions` with `{'queries': ['semiconductor industry stock market updates', \"latest news on chip manufacturers' shares\", 'recent developments in semiconductor company stocks', 'stock performance news for companies like NVIDIA and AMD', 'market trends in semiconductor stocks such as Intel and Qualcomm', \"updates on semiconductor firms' share prices and financial news\"], 'max_results': 15}`\n",
            "\n",
            "\n",
            "\u001b[0m\u001b[33;1m\u001b[1;3m[{'content': \"## A Very Rapid' Drop In Domestic Demand Is Hitting Airlines\\n\\n\\nComplement to a former post. \\nAny jump in airlines stock price is just dancing around for now\\n\\nThe Travel crisis that is emerging will affect other industries that profit from tourism, like real estate and Banks\\n\\nhttps://finance.yahoo.com/news/very-rapid-drop-domestic-demand-182820043.html\\n\\nhttps://www.reddit.com/r/wallstreetbets/s/7nEvEsNjpp\\n\\nhttps://sherwood.news/business/us-airlines-in-a-recession-southwest-ceo/\\n\\nOn the international front, for majors Airlines, Chinese and other Asian international tourists are crucial to their long-haul operations. Shanghai Pudong round trip to Dallas-Fort Worth is a common route that is seing drastic decrease for AAL.\\n\\nA downturn in both leisure and business tourism leads to reduced hotel occupancy rates, lower room pricing, and decreased revenue, particularly in tourist-centric cities like Las Vegas, Orlando, and New York City.\\n\\nCompanies/stock potentially affected: Host Hotels &amp; Resorts Inc., Park Hotels &amp; Resorts Inc. \\n\\nTenet Healthcare (THC) – Dallas, TX maybe affcted too. They owns hospitals and medical centers, some of which attract foreign patients for medical tourism.\\n\\nCarnival Corporation (CCL) – Miami, FL, world’s largest cruise line, heavily dependent on international tourists, including Chinese travelers.\\n\\nRoyal Caribbean Group (RCL) – Miami, FL, same as Carnival, with an even more aggressive focus on Asia-Pacific expansion, including China-based cruise itineraries.\\n\\nMany Buffalo/NY small businesses rely on Canadian spending. If revenue drop, they cant pay rent, if they cant pay rent, landlords cant service mortage debt. More businesses will close like in covid or after covid.\\n\\nLennar Corporation (LEN) – Miami, FL, one of the largest homebuilders in the U.S has lot of Chinese buyers: Florida (and Texas) have been targets for foreign real estate investment, particularly by Chinese nationals seeking EB-5 visas or U.S. property. So real estate may be affected too, in this case, may be it will relieve pressure on prices, not sure. But for REITS (real estate investment trusts) that owns properties leased to Hotels and restaurants or Malls or international student housing that depends on international travellers or Transit/transcient business, trouble are ahead in all major US Cities. \\n\\nEvent Spaces &amp; Convention Centers: A reduction in MICE (Meetings, Incentives, Conferences, Exhibitions) activities weakens demand for event spaces and the surrounding businesses that support them. ALL major hotels brand affected.\\n\\nDiversified REITs: These entities, with portfolios spanning retail, hotel, and mixed-use developments in major gateway cities, face indirect impacts from the downturn in tourism and international student presence: Vornado Realty Trust (VNO), BXP, Inc. (BXP)\\n\\nThe third financed asset in size for all major banks after government bonds and other bank is real estate ( commercial and residential)\\n\\nalso, if all foreign countries keep rejecting boeing planes delivery like china, it is the whole aerospace sector that is going down with all of the small industries and SMEs that their business model feeds. \\n\\nhttps://fortune.com/article/boeing-ceo-trump-china-tariff-trade-war-planes-economy/\\n\\nWell no wonder T is saying now: we are discussing with China, but they said: heuuu, no we are not discussing yet. remove all tariffs first, holder of negotiation power is obvious. So until all of this is fixed and factoring at least few months of starring, the crash is coming.\\n\\nAnd we have not even talked about the soccer world cup being potentially cancelled in a year by the US, by FiFa or by implicit boycott by countries outside of Usa because not safe and impact on existing contracts with airlines, hotels and hosting cities that have spent millions\\n\\n\\nI won’t hold a high position when shorting airlines, since they are highly volatile and already close to 52 weeks low. but I will average up a short position. Should back test Covid trading range volatility to see how low it can get\", 'subreddit': 'wallstreetbets', 'score': 7.87915039, 'url': 'https://reddit.com/r/wallstreetbets/comments/1k71daq', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## Tesla reports 20% Q1 drop in auto revenue\\n\\nBrutal numbers from Tesla after the bell. \\n\\nAs we all know, their stock performance is often decoupled from results. Little movement in futures so far, but curious how it’ll move tomorrow. \\n\\nTotal revenue slid 9% from $21.3 billion a year earlier. Automotive revenue dropped 20% to $14 billion from $17.4 billion in the same period last year.\\n\\nTesla said one reason for the decline was the need to update lines at its four vehicle factories to start making a refreshed version of its popular Model Y SUV. The company also pointed to lower average selling prices and sales incentives as a drag on revenue and profit.\\n\\nNet income plummeted 71% to $409 million, or 12 cents a share, from $1.39 billion or 41 cents a year ago.\\n\\n…\\n\\nThe company refrained from promising growth this year and said it will “revisit our 2025 guidance in our Q2 update.”\\n\\nhttps://www.cnbc.com/2025/04/22/tesla-tsla-earnings-report-q1-2025.html\\n\\nETA: it’s now up a staggering 3% after delivering that terrible news - they must be into the vaporware portion of the call.', 'subreddit': 'investing', 'score': 6.59399414, 'url': 'https://reddit.com/r/investing/comments/1k5htk8', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## Tariff\\'s: When “America First” Means “Jobs Last”( as Stock Market is Down\\xa019%\\xa0)\\n\\nSo, President Trump’s tariff strategy is back in full swing, and the results are... well, let’s just say the job market is experiencing a plot twist: **Stock Market is** Down\\xa0**19%**\\xa0since February\\xa0which leads t0 :\\n\\n1. **Stellantis**\\xa0– Temporarily laid off\\xa0**900 workers**\\xa0\\n2. **Volvo**\\xa0– Cutting\\xa0**800 U.S. jobs**\\xa0\\n3. **Cleveland Cliffs (Steel Industry)**\\xa0–\\xa0**1,200 workers**\\xa0got the boot because tariffs on steel imports made their business model\\xa0*\"*\\n\\nAccording to Goldman Sachs, while tariffs might create around 100,000 manufacturing jobs, they could also lead to the loss of approximately 500,000 jobs in other sectors.\\n\\nIf your investments are in manufacturing, exports, or anything that involves the word \"import,\" now might be a great time to explore yoga. Or joining mental health counsling.\\n\\nSo yes, markets *technically* still function but between layoffs, production freezes, and trade diplomacy via press conference, Wall Street is just hoping someone hits “stop” before we all end up wuth this stupidty\\n\\nSource: [https://www.forbes.com/sites/rachelwells/2025/04/09/what-jobs-will-be-impacted-by-trumps-tariffs-in-2025/](https://www.forbes.com/sites/rachelwells/2025/04/09/what-jobs-will-be-impacted-by-trumps-tariffs-in-2025/)', 'subreddit': 'stocks', 'score': 6.42871094, 'url': 'https://reddit.com/r/stocks/comments/1k6pcc7', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## Stock futures slip after China says trade talks haven’t even started.\\n\\nStock futures slipped Thursday after China said that it had no ongoing trade talks with the U.S., dashing investors’ hopes of an ease in tensions between the two nations. \\n“At present there are absolutely no negotiations on the economy and trade between China and the U.S.,” said Ministry of Commerce Spokesperson He Yadong.\\n\\nhttps://www.cnbc.com/2025/04/23/stock-market-today-live-updates.html', 'subreddit': 'investing', 'score': 6.15234375, 'url': 'https://reddit.com/r/investing/comments/1k6qouk', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## Intel CFO says tariffs increase chance for economic slowdown, recession getting likelier\\n\\nSource: [https://www.cnbc.com/2025/04/24/intel-cfo-says-tariffs-raise-chance-for-economic-slowdown-recession.html](https://www.cnbc.com/2025/04/24/intel-cfo-says-tariffs-raise-chance-for-economic-slowdown-recession.html)\\n\\n&gt;  \\nIntel\\xa0CFO David Zinsner said\\xa0President Donald Trump’s\\xa0tariffs and retaliation from other countries has increased the likelihood of a recession.\\n\\n&gt;“The very fluid trade policies in the U.S. and beyond, as well as regulatory risks, have increased the chance of an economic slowdown, with the probability of a recession growing,” Zinsner said on the company’s quarterly earnings call on Thursday.\\n\\n&gt;Intel reported better-than-expected\\xa0first-quarter results, partially because some customers stockpiled chips ahead of tariffs, the company said. However, guidance for revenue and profit was below expectations, pushing the chipmaker’s stock down more than 5% in extended trading.\\n\\n&gt;Intel’s forecast for the current quarter is $11.2 billion to $12.4 billion. Zinsner said the range is “wider than normal” due to uncertainty caused by tariffs.\\n\\n  \\nIntel down -5% overnight. Looks like a gloomy outlook from the chip maker. ', 'subreddit': 'stocks', 'score': 5.9855957, 'url': 'https://reddit.com/r/stocks/comments/1k796l2', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## SS Innovations to Uplist to Nasdaq\\n\\nNews Link: [https://www.globenewswire.com/news-release/2025/04/24/3067417/0/en/SS-Innovations-to-Uplist-to-Nasdaq.html](https://www.globenewswire.com/news-release/2025/04/24/3067417/0/en/SS-Innovations-to-Uplist-to-Nasdaq.html)\\n\\n  \\nFORT LAUDERDALE, Fla., April 24, 2025 (GLOBE NEWSWIRE) -- [**SS Innovations International, Inc.**](https://www.globenewswire.com/Tracker?data=2JGWgvztYvzBB6B3hP2PuBqN2NwaTepB3EDTsNwuIZ7S0SD2APsUcHFyoqtgBd6hYfIC81SZpVJgixhFUK0HfJD6rd27QvAXsztAmpH14tELe5ANn3jSu_JFE2o8CCco) (the “Company” or “SS Innovations”) (OTC: SSII), a developer of innovative surgical robotic technologies dedicated to making robotic surgery affordable and accessible to a global population, today announced that the Company’s common stock has been approved for listing on The Nasdaq Capital Market® (“Nasdaq”). Trading on Nasdaq is expected to commence on April 25, 2025, and the Company’s shares will continue to be listed under the ticker symbol “SSII”.\\n\\nDr. Sudhir Srivastava, Chairman of the Board and Chief Executive Officer of SS Innovations, commented, “Uplisting to Nasdaq marks an exciting milestone for SS Innovations and reflects our team’s achievements in building a world-class robotic surgery technology platform that is designed to offer top-tier quality, safety, efficiency and affordability, enabling cutting-edge robotic surgical procedures for a wider range of patients globally.\\n\\n“The timing of our uplisting coincides with the beginning of our global expansion beyond India – into Nepal, Ecuador, Guatemala, the Philippines, Indonesia, Sri Lanka and Ukraine – and our planned strategic entry into Europe and the United States. We are pursuing EU CE Mark and U.S. FDA approval for our SSi Mantra surgical robotic system, which we anticipate in late 2025 and early 2026, respectively. We believe that the Nasdaq uplisting will enhance market awareness of our growth story, improve transparency, expand our audience of potential investors, and ultimately increase share liquidity.”\\n\\nDr. Srivastava also highlighted the Company’s recent achievements in advancing its clinically validated, patented surgical robotic system, the “SSi Mantra,” including:\\n\\n* Installing 80 SSi Mantra systems in 75 hospitals across India and other countries;\\n* 3,500+ surgeries performed using the SSi Mantra, including over 195 robotic cardiac surgeries with zero reported device-related mortality, injuries, or complications;\\n* Becoming the first and only company in India to receive Central Drugs Standard Control Organization (CDSCO, India’s equivalent of the U.S. FDA) regulatory approval for use of a robotic surgical system in the performance of telesurgery and tele-proctoring procedures. Since receiving such approvals, the Company has performed 16 telesurgeries using SSi Mantra, including the world’s first robotic cardiac telesurgeries over distances up to 1,200 miles; and\\n* Reporting revenue for the year ended December 31, 2024 of $20.6 million, an approximately 251% increase from 2023 revenue of $5.9 million, and gross margin of 40.9%, an increase from 12.3% in 2023.', 'subreddit': 'pennystocks', 'score': 5.70605469, 'url': 'https://reddit.com/r/pennystocks/comments/1k6rzos', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': \"## Southwest to cut flights this year, pulls guidance, citing 'macroeconomic uncertainty'. Airlines is a fixed cost industry\\n\\nlike jetblue, southwest usually relies on its canadian customer base for some destinations; not that it is very important, but small variations can make big differences. Macroeconomic uncertainties means, We don’t really know sh…t about where we are going. Well, thank you pilot! Full effect of tariffs and bullying will kick in Q3 earnings release\\n\\nhttps://www.cnbc.com/2025/04/23/southwest-airlines-luv-earnings-q1-2025.html\\n\\nhttps://finance.yahoo.com/news/very-rapid-drop-domestic-demand-182820043.html\\n\\n\\nhttps://sherwood.news/business/us-airlines-in-a-recession-southwest-ceo/\\n\\nAirlines and Hospitality are high fixed cost Industries( the one you still incur with or without revenue); unless you are operating on a very limited service business model, Low contribution margin and high fixed cost are bad for even small variations in load factor and occupancy rates. Break even point is around 60 to 65 percent annualized load factor or occupancy rates for low cost, higher for middle to upper scale. Airlines flagship survive only because of national pride and government involvement . Also, These industries are good proxies to assess if we are in recession, the depth and if we are recovering. By all adjacent metrics ( skilled/quality mix, the one that pay high price, i.e business customers and meeting incentive conference exhibitions) and leisure customer ( low paying) we are already in recession. It will fully show in other hospitality companies, airlines and other industries in Q2 earnings. You may want to check the latest Management discussion and analysis of these companies ( and possible that they stay silent or conservative to avoid stock beating) we are going for a bumpy ride, tariffs or not tariffs unless the US customer can survive with The USA in isolation. Inflation up because of tariffs and unemployment up soon; where is monetary policy going? Stock market, for all stocks may be except nvidia, not enough sustainable catalyst to go in the middle of the 52 weeks range, overall more downside than upside. Stock are going up? sure, option cleaning, just dancing .Update after q2 and q3 earnings release..for full impact. \\nOverall, from the Top, a very very bigly bad chess, where the second piece moved was the king, and the king hasn’t stopped moving since in all direction, and the dragon is secretly smiling \\n\\nfor the redditor challenging the statement that airlines operate in mainly fixed cost industry ( you still have to pay aircraft lease, airport fees, routine maintenance and maintenance capex, air route rights, craft parking and tarmac fees, some salaries lines will stay fixed), do your due diligence, this link is for you https://www.spsairbuz.com/story/?id=964 \\n post updated here : \\n\\nhttps://www.reddit.com/r/wallstreetbets/s/6U6MnuzJsr\", 'subreddit': 'wallstreetbets', 'score': 5.62988281, 'url': 'https://reddit.com/r/wallstreetbets/comments/1k6e5bf', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## The real reason Trump was pushing so hard for interest rate cuts - The housing market is in trouble...\\n\\nJust search for [housing market....](https://www.google.com/search?q=housing+market&amp;oq=housing+ma&amp;gs_lcrp=EgZjaHJvbWUqDggAEEUYJxg7GIAEGIoFMg4IABBFGCcYOxiABBiKBTIGCAEQRRhAMgYIAhBFGDkyDQgDEAAYkQIYgAQYigUyDQgEEAAYkQIYgAQYigUyBggFEEUYPDIGCAYQRRg8MgYIBxBFGDzSAQgxMTkxajBqMagCCLACAfEFho40eTWzz_M&amp;sourceid=chrome&amp;ie=UTF-8) \\n\\nTariffs can be turned on and off, a slowdown in the housing market can be long term destructive and takes years to correct (think 2009). Most all the larger homebuilding stocks are down. Higher interest rates will continue to put downward pressure on housing that is %16 of GDP.  \\n\\nSome regions will do fine. Most of the rest of the country with rising insurance rates, affordability, the slow elimination of the protections of FEMA will grind hard on the housing market and the many people who work in that industry.\\n\\nCASH', 'subreddit': 'stocks', 'score': 5.5526123, 'url': 'https://reddit.com/r/stocks/comments/1k6x0ln', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': \"## Gold stocks hitting $3,300/oz\\n\\nBack in early January, I saw a note saying gold might finally have its moment in 2025. At the time, gold was around $2,600 an ounce, and most people were still focused on tech and AI stocks. Honestly, I didn’t think much of it.\\n\\nNow it’s April, and gold just crossed $3,330. That’s a 27% jump in four months. And suddenly, fund managers are calling gold the most crowded trade on Wall Street beating out tech for the first time in two years.\\n\\nA lot happened, Inflation hasn’t cooled down as much as people hoped. There’s still a ton of uncertainty around interest rates. And global tensions whether it’s Ukraine, the Middle East, or China have pushed a lot of people toward what they see as “safer” investments.\\n\\nInstead of buying physical gold or a gold ETF, I started looking into gold mining companies. The basic idea is this: when gold prices go up, mining profits usually go up even faster. That’s because their costs stay fairly flat while the value of what they’re digging up increases. So you get more upside though more risk too.\\n\\nAccording to my research there are three types of gold stocks I’ve been looking at:\\n\\n1. Major producers — big mining companies with steady operations.\\n2. Junior miners — smaller companies that might hit it big with a new discovery, but come with more risk.\\n3. Streaming companies — these guys don’t mine gold themselves. They give money to miners upfront and get a portion of the gold in return. Less risk, but less reward too.\\n\\n  \\nHere's what I've been tracking:\\n\\nWheaton Precious Metals (WPM)  \\nThis one is a streaming company, so it doesn’t operate mines—it collects a share of gold from others. It’s a lower-risk model and still benefits from rising gold prices. Production is up, and they’re projecting long-term growth. The stock’s up over 50%.\\n\\nAgnico Eagle (AEM)  \\nSolid company with operations in Canada, Australia, and a few other places. They’ve been steadily growing reserves and investing in new exploration projects. Up nearly 90% this year.\\n\\nKinross Gold (KGC)  \\nIt’s up over 120% so far this year. They’ve been selling off riskier assets and doubling down on stable regions like the U.S., Canada, and Brazil. They’re also using AI and automation to cut costs. It’s starting to pay off.\\n\\nGold stocks have clearly run up. Valuations on some names especially the streaming companies are looking expensive. But at the same time, demand is still strong, and a lot of people see gold as a safe bet if things get worse economically or politically.\\n\\n  \\nWould it be safer to just buy a gold ETF instead? Something like GLD or IAU?\\n\\n\\n\\n  \\n\", 'subreddit': 'investing', 'score': 5.53735352, 'url': 'https://reddit.com/r/investing/comments/1k7e847', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## New Gallup Poll shows a Majority of Americans Feel their economic situation will be getting worse\\n\\n\\nGallup’s yearly reading on Americans’ assessment of their personal finances shows a record-high 53% now believing their situation is getting worse. This marks the first time in the trend dating back to 2001 that a majority have expressed financial pessimism.\\n\\nhttps://news.gallup.com/poll/659630/americans-economic-financial-expectations-sink-april.aspx', 'subreddit': 'stocks', 'score': 5.2121582, 'url': 'https://reddit.com/r/stocks/comments/1k6q3ov', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## r/Stocks Daily Discussion &amp; Options Trading Thursday - Apr 24, 2025\\n\\nThis is the daily discussion, so anything stocks related is fine, but the theme for today is on stock options, but if options aren\\'t your thing then just ignore the theme.\\n\\nSome helpful day to day links, including news:\\n\\n* [Finviz](https://finviz.com/quote.ashx?t=spy) for charts, fundamentals, and aggregated news on individual stocks\\n* [Bloomberg market news](https://www.bloomberg.com/markets)\\n* StreetInsider news:\\n  * [Market Check](https://www.streetinsider.com/Market+Check) - Possibly why the market is doing what it\\'s doing including sudden spikes/dips\\n  * [Reuters aggregated](https://www.streetinsider.com/Reuters) - Global news\\n\\n-----\\n\\nRequired info to start understanding options:\\n\\n* [Call option Investopedia video](https://www.investopedia.com/terms/c/calloption.asp) basically a call option allows you to buy 100 shares of a stock at a certain price (strike price), but without the obligation to buy\\n* [Put option Investopedia video](https://www.investopedia.com/terms/p/putoption.asp) a put option allows you to sell 100 shares of a stock at a certain price (strike price), but without the obligation to sell\\n* Writing options switches the obligation to you and you\\'ll be forced to buy someone else\\'s shares (writing puts) or sell your shares (writing calls)\\n\\nSee the following word cloud and click through for the wiki:\\n\\n[Call option - Put option - Exercising an option - Strike price - ITM - OTM - ATM - Long options - Short options - Combo - Debit - Credit or Premium - Covered call - Naked - Debit call spread - Credit call spread - Strangle - Iron condor - Vertical debit spreads - Iron Fly](https://www.reddit.com/r/stocks/wiki/options-themed-post)\\n\\nIf you have a basic question, for example \"what is delta,\" then google \"investopedia delta\" and click the investopedia article on it; do this for everything until you have a more in depth question or just want to share what you learned.\\n\\nSee our past [daily discussions here.](https://www.reddit.com/r/stocks/search?q=author%3Aautomoderator+%22r%2Fstocks+daily+discussion%22&amp;restrict_sr=on&amp;sort=new&amp;t=all)  Also links for:  [Technicals](https://www.reddit.com/r/stocks/search?q=author%3Aautomoderator+title%3Atechnicals&amp;restrict_sr=on&amp;include_over_18=on&amp;sort=new&amp;t=all) Tuesday, [Options Trading](https://www.reddit.com/r/stocks/search?q=author%3Aautomoderator+title%3Aoptions&amp;restrict_sr=on&amp;include_over_18=on&amp;sort=new&amp;t=all) Thursday, and [Fundamentals](https://www.reddit.com/r/stocks/search?q=author%3Aautomoderator+title%3Afundamentals&amp;restrict_sr=on&amp;include_over_18=on&amp;sort=new&amp;t=all) Friday.', 'subreddit': 'stocks', 'score': 5.16601562, 'url': 'https://reddit.com/r/stocks/comments/1k6o0i8', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': \"## Rate My Portfolio - r/Stocks Quarterly Thread March 2025\\n\\nPlease use this thread to discuss your portfolio, learn of other stock tickers &amp; portfolios like [Warren Buffet's](https://buffett.online/en/portfolio/), and help out users by giving constructive criticism.\\n\\nWhy quarterly?  Public companies report earnings quarterly; many investors take this as an opportunity to rebalance their portfolios.  We highly recommend you do some reading:  Check out our wiki's list of [relevant posts &amp; book recommendations.](https://www.reddit.com/r/stocks/wiki/index/#wiki_relevant_posts.2C_books.2C_wiki_recommendations)\\n\\nYou can find stocks on your own by using a scanner like your broker's or [Finviz.](https://finviz.com/screener.ashx)  To help further, here's a list of [relevant websites.](https://www.reddit.com/r/stocks/wiki/index/#wiki_relevant_websites.2Fapps)\\n\\nIf you don't have a broker yet, see our [list of brokers](https://www.reddit.com/r/stocks/wiki/index/#wiki_brokers_for_investing) or search old posts.  If you haven't started investing or trading yet, then setup your [paper trading to learn basics like market orders vs limit orders.](https://www.reddit.com/r/stocks/wiki/index/#wiki_is_there_a_way_to_practice.3F)\\n\\nBe aware of [Business Cycle Investing](https://eresearch.fidelity.com/eresearch/markets_sectors/sectors/si_business_cycle.jhtml?tab=sibusiness) which Fidelity issues updates to the state of global business cycles every 1 to 3 months (note: Fidelity changes their links often, so search for it since their take on it is enlightening).  [Investopedia's take on the Business Cycle](https://www.investopedia.com/articles/investing/061316/business-cycle-investing-ratios-use-each-cycle.asp).\\n\\nIf you need help with a falling stock price, check out Investopedia's [The Art of Selling A Losing Position](https://www.investopedia.com/articles/02/022002.asp) and their [list of biases.](https://www.investopedia.com/articles/stocks/08/capital-losses.asp)\\n\\nHere's a list of all the [previous portfolio stickies.](https://www.reddit.com/r/stocks/search?q=author%3Aautomoderator+title%3A%22Rate+My+Portfolio%22&amp;restrict_sr=on&amp;sort=new&amp;t=all)\", 'subreddit': 'stocks', 'score': 5.14550781, 'url': 'https://reddit.com/r/stocks/comments/1j0w73o', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': '## Mr Market Where are you? on FLGT?\\n\\nI don’t usually post investment ideas publicly, but as someone who thinks statistically (maybe obsessively so), I’ve been wrestling with an interesting case: Fulgent Genetics (FLGT).\\n\\nIt’s trading under $20, despite holding around $25 per share in cash (what? I can’t get over this, plus really high revenue per share plus upside from development pipeline in both dna diagnostics testing and in early phase pharmaceuticals). From my analysis, at a minimum, I keep coming back to $32 per share as fair value right now for revenue not counting upside potential, up to $42 per share based on their latest financial postings.\\n\\nI’ve spent more time than I’d like to admit analyzing this stock. It’s kind of embarrassing, actually—like, even if I’m binge-watching Netflix, part of my brain is still secretly running valuation scenarios 😂 \\n\\nSeriously, though, I’ve thought extensively about this, especially through a Kurzweilian lens on how AI-driven technology will likely reshape biotech (I’m thinking that starts with testing, where pattern detection is critical, and AI is good at).\\n\\nI genuinely think it’s very grossly undervalued and that time will tell. Markets are supposed to make sense and this doesn’t make sense! “Mr. Market where are you?”, even though I could certainly be wrong, I have a lot of theories as to why this is undervalued that I don’t wanna mention because I don’t want to bias anyone. I’d love to hear other perspectives on why the market might be overlooking this, if anyone wouldn’t mind giving it a go.\\n\\n(Full disclosure: I’ve been buying shares periodically over the past year, so take my enthusiasm with a grain of salt)', 'subreddit': 'investing', 'score': 5.00683594, 'url': 'https://reddit.com/r/investing/comments/1k79k42', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': \"## RHCO big news out today.. Finalized ACQUISITION of Morrich Lottery Limited and secured LOTTERY, SPORTSBOOK, and CASINO LICENSES\\n\\nThis stock has very little volume and its a small float. But looks good with their current financials and this recent completed acquisition to add more to the books.\\n\\nHere's a quick look at their financials from last quarter:\\n\\n•$5.7M in revenue last quarter\\n\\n•Net profitable\\n\\n•$12.9M in assets\\n\\n•No convertible notes\\n\\n•Small float, 24M shares held at DTC (The held at DTC number is the public tradable float)\\n\\n•Completed acquisition news today, (Finalized ACQUISITION of Morrich Lottery Limited and secured lottery, sportsbook and casino licenses)\", 'subreddit': 'unknown', 'score': 4.87890625, 'url': '', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}, {'content': \"## Don't Sleep on Damon (DMN)\\n\\nThis two-wheel electric vehicle company could make for a quick return. Currently, the stock price sits at $0.0035 (as I type this), but they have $100 million in pre-orders for their two motorcycle models, which include safety and accessibility tech you'd find in newer four-wheel vehicles. They're also big on data intelligence, and are open to working with other companies to share info, such as supporting ICE (internal combustion engine) companies to transition. They presented at the Planet MicroCap Showcase just yesterday, which is available online: [https://event.summitcast.com/view/YNz6mnmEsXyrdRxb78w2nX/9WosXgUD44wqvJunNd982Y](https://event.summitcast.com/view/YNz6mnmEsXyrdRxb78w2nX/9WosXgUD44wqvJunNd982Y) (Or just Google it if you don't trust links.) Among other things, they state the global market for electric bikes is forecasted to reach $40 billion by 2030. While the US may have an administration trying to move in the opposite direction of electrification, the world thinks otherwise!\", 'subreddit': 'pennystocks', 'score': 4.86328125, 'url': 'https://reddit.com/r/pennystocks/comments/1k70v8k', 'matched_query': \"updates on semiconductor firms' share prices and financial news\"}]\u001b[0m\u001b[32;1m\u001b[1;3mHere's a summary of what Reddit users are saying about the recent performance of semiconductor companies' stocks:\n",
            "\n",
            "1. Tariffs and Trade Tensions: There is significant concern about the impact of renewed tariffs and trade tensions, especially between the US and China. Intel's CFO recently stated that tariffs have increased the likelihood of an economic slowdown or recession, and this uncertainty has led to a wider-than-normal revenue forecast range for Intel. The company’s stock dropped more than 5% after its latest earnings call, despite beating expectations, due to cautious guidance and tariff-related risks.\n",
            "\n",
            "2. Stock Performance: The overall sentiment is cautious to negative for semiconductor stocks in the short term. Intel’s stock specifically took a hit after its earnings, and there is a general sense that the sector is facing headwinds from macroeconomic uncertainty, trade policy, and potential slowdowns in demand.\n",
            "\n",
            "3. Broader Market Impact: Some posts mention that, aside from a few exceptions like NVIDIA, most stocks (including semiconductors) are struggling to find sustainable catalysts for upward movement. The market is seen as having more downside risk than upside at the moment, with many investors waiting for further clarity after Q2 and Q3 earnings.\n",
            "\n",
            "4. Demand and Inventory: Intel noted that some of its recent sales were driven by customers stockpiling chips ahead of tariffs, which could mean softer demand in future quarters as inventories are worked through.\n",
            "\n",
            "5. Company-Specific News: While most discussion is about large players like Intel, NVIDIA, and AMD, there is also mention of smaller tech and semiconductor-related companies making moves, such as SS Innovations uplisting to Nasdaq, but this is more about company milestones than sector-wide trends.\n",
            "\n",
            "In summary, Reddit users are currently wary of semiconductor stocks due to trade policy risks, macroeconomic uncertainty, and cautious corporate guidance. Intel’s recent earnings and commentary have amplified concerns about the sector’s near-term outlook, and most investors are taking a wait-and-see approach until more data comes in from upcoming earnings reports.\u001b[0m\n",
            "\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "data": {
            "text/markdown": [
              "Here's a summary of what Reddit users are saying about the recent performance of semiconductor companies' stocks:\n",
              "\n",
              "1. Tariffs and Trade Tensions: There is significant concern about the impact of renewed tariffs and trade tensions, especially between the US and China. Intel's CFO recently stated that tariffs have increased the likelihood of an economic slowdown or recession, and this uncertainty has led to a wider-than-normal revenue forecast range for Intel. The company’s stock dropped more than 5% after its latest earnings call, despite beating expectations, due to cautious guidance and tariff-related risks.\n",
              "\n",
              "2. Stock Performance: The overall sentiment is cautious to negative for semiconductor stocks in the short term. Intel’s stock specifically took a hit after its earnings, and there is a general sense that the sector is facing headwinds from macroeconomic uncertainty, trade policy, and potential slowdowns in demand.\n",
              "\n",
              "3. Broader Market Impact: Some posts mention that, aside from a few exceptions like NVIDIA, most stocks (including semiconductors) are struggling to find sustainable catalysts for upward movement. The market is seen as having more downside risk than upside at the moment, with many investors waiting for further clarity after Q2 and Q3 earnings.\n",
              "\n",
              "4. Demand and Inventory: Intel noted that some of its recent sales were driven by customers stockpiling chips ahead of tariffs, which could mean softer demand in future quarters as inventories are worked through.\n",
              "\n",
              "5. Company-Specific News: While most discussion is about large players like Intel, NVIDIA, and AMD, there is also mention of smaller tech and semiconductor-related companies making moves, such as SS Innovations uplisting to Nasdaq, but this is more about company milestones than sector-wide trends.\n",
              "\n",
              "In summary, Reddit users are currently wary of semiconductor stocks due to trade policy risks, macroeconomic uncertainty, and cautious corporate guidance. Intel’s recent earnings and commentary have amplified concerns about the sector’s near-term outlook, and most investors are taking a wait-and-see approach until more data comes in from upcoming earnings reports."
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "from IPython.display import Markdown, display\n",
        "\n",
        "query = \"What happened to semiconductor companies' stock recently?\"\n",
        "\n",
        "# run the agent with the query\n",
        "result = agent_executor.invoke({\"input\": query})\n",
        "\n",
        "display(Markdown(result['output']))"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "6cfa133a-7a71-4a3c-a8b0-ad6ce4720e3d",
      "metadata": {
        "id": "6cfa133a-7a71-4a3c-a8b0-ad6ce4720e3d"
      },
      "source": [
        "## How does our search perform without query expansion?\n",
        "\n",
        "Let's see what we'd be missing out on if we weren't using query expansion in our search.\n",
        "\n",
        "We'll set up another agent that doesn't have access to the query expansion tool."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 28,
      "id": "OroMJSv_GOjN",
      "metadata": {
        "id": "OroMJSv_GOjN"
      },
      "outputs": [],
      "source": [
        "memory2 = ConversationBufferMemory(\n",
        "    memory_key=\"chat_history\",\n",
        "    return_messages=True\n",
        ")\n",
        "\n",
        "# this time initialize with ONLY the reddit_stock_mentions\n",
        "agent_wo_expansion = create_openai_tools_agent(\n",
        "    llm=main_llm, tools=[reddit_stock_mentions], prompt=prompt\n",
        ")\n",
        "\n",
        "# package into another agent executor\n",
        "agent_executor = AgentExecutor(\n",
        "    agent=agent_wo_expansion,\n",
        "    tools=[reddit_stock_mentions],\n",
        "    memory=memory2,\n",
        "    verbose=True,\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "in0Mo8v1YRAz",
      "metadata": {
        "id": "in0Mo8v1YRAz"
      },
      "source": [
        "Now let's try the same query _without_ the query expansion tool _and_ while telling the agent just use a single query:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 30,
      "id": "yavX917bGfCr",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 443
        },
        "id": "yavX917bGfCr",
        "outputId": "7212563b-9596-4c12-a102-e401ccd0e3bc"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "\n",
            "\n",
            "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
            "\u001b[32;1m\u001b[1;3m\n",
            "Invoking: `reddit_stock_mentions` with `{'queries': ['semiconductor stocks recent performance'], 'max_results': 10}`\n",
            "\n",
            "\n",
            "\u001b[0m\u001b[36;1m\u001b[1;3m[{'content': '## USAS : Q1 Earnings Estimate for Americas Silver Issued By Cormark\\n\\nAmericas Silver Corp ([NYSEAMERICAN:USAS](https://www.marketbeat.com/stocks/NYSEAMERICAN/USAS/)\\xa0–\\xa0[Free Report](https://www.marketbeat.com/arnreports/ReportTickerOptin.aspx?RegistrationCode=TickerHyperlink&amp;Prefix=NYSEAMERICAN&amp;Symbol=USAS)) – Investment analysts at Cormark issued their Q1 2025 earnings per share (EPS) estimates for shares of Americas Silver in a research report issued to clients and investors on Tuesday, April 22nd. Cormark analyst N. Dion expects that the company will post earnings per share of $0.00 for the quarter. Cormark currently has a “Moderate Buy” rating on the stock. The consensus estimate for Americas Silver’s current full-year earnings is ($0.11) per share.\\xa0\\n\\nSeparately, HC Wainwright boosted their price target on shares of Americas Silver from $0.90 to $1.00 and gave the stock a “buy” rating in a research report on Friday, March 28th.\\n\\n# Americas Silver Price Performance\\n\\nShares of\\xa0[USAS stock](https://www.marketbeat.com/stocks/NYSEAMERICAN/USAS/earnings/)\\xa0opened at $0.62 on Wednesday. The stock has a market cap of $153.43 million, a PE ratio of -3.44 and a beta of 1.20. Americas Silver has a 12 month low of $0.21 and a 12 month high of $0.65. The company has a current ratio of 0.42, a quick ratio of 0.28 and a debt-to-equity ratio of 0.18.\\n\\n# Institutional Trading of Americas Silver\\n\\nHedge funds have recently added to or reduced their stakes in the stock. HighTower Advisors LLC bought a new stake in Americas Silver in the 4th quarter valued at about $47,000. American Century Companies Inc. bought a new stake in shares of Americas Silver in the fourth quarter valued at approximately $73,000. Bank of America Corp DE raised its position in shares of Americas Silver by 273,321.1% in the fourth quarter. Bank of America Corp DE now owns 207,800 shares of the company’s stock valued at $78,000 after purchasing an additional 207,724 shares during the period. JPMorgan Chase &amp; Co. purchased a new stake in shares of Americas Silver during the 4th quarter valued at approximately $94,000. Finally, Cetera Investment Advisers boosted its holdings in Americas Silver by 4.8% in the 4th quarter. Cetera Investment Advisers now owns 428,844 shares of the company’s stock worth $167,000 after buying an additional 19,696 shares during the period. Institutional investors own 13.38% of the company’s stock.\\xa0\\n\\n# About Americas Silver\\n\\nAmericas Gold and Silver Corporation engages in the acquisition, evaluation, exploration, development, and operation of mineral properties. The company explores for silver, lead, zinc and copper. It principally owns 100% interests in the Cosalá Operations consisting of 67 mining concessions that cover approximately 19,385 hectares located in the state of Sinaloa, Mexico; and Galena Complex situated near the town of Wallace in the state of Idaho, the United States.\\n\\n[https://www.defenseworld.net/2025/04/24/q1-earnings-estimate-for-americas-silver-issued-by-cormark.html](https://www.defenseworld.net/2025/04/24/q1-earnings-estimate-for-americas-silver-issued-by-cormark.html)', 'subreddit': 'pennystocks', 'score': 4.15527344, 'url': 'https://reddit.com/r/pennystocks/comments/1k6sd43', 'matched_query': 'semiconductor stocks recent performance'}, {'content': '## Retail buys the dip, and the top, again!\\n\\nThis Bloomberg article succinctly describes how the smart and big investors manipulate the market by selling their stock to the retail investors like you and me, in a bear market. \\nhttps://www.bloomberg.com/news/articles/2025-04-23/retail-traders-see-epic-buying-opportunity-in-s-p-s-wild-swings\\n\\n\"It is indeed true that the major equity benchmarks in the US rise in the long term. However, it is also true that there are windows within that trajectory where holding stocks over periods even for several years would still not let an investor break even on the initial investment. A recent example was in the dotcom bubble, when someone who bought into the S&amp;P 500 at the top in early 2000 did not see any return until mid-2007\"\\n\\nThis initial volatility continues for a few months, when retail investors run out of steam. That\\'s when the real \\'bear market\\' starts. \\n\\nAnyone who\\'s bullish right now should really, really do some more research. \\n', 'subreddit': 'stocks', 'score': 3.57519531, 'url': 'https://reddit.com/r/stocks/comments/1k6fmg0', 'matched_query': 'semiconductor stocks recent performance'}, {'content': '## What’s your strategy on selling stocks?\\n\\nHi guys, I was wondering when you decide when to sell a stock in your portfolio\\n\\nI’m asking because there are times where my stocks are in the green and I’m not sure whether to sell or keep holding. Obviously I like to take profits but sometimes I fear I’ll miss out on bigger returns.\\n\\nSo do  guys you sell when you need the cash? Or when you’ve held the stock for over a year? Or you guys just hold until you realize the stock is not going anywhere?\\n\\n', 'subreddit': 'investing', 'score': 3.18164062, 'url': 'https://reddit.com/r/investing/comments/1k6wi48', 'matched_query': 'semiconductor stocks recent performance'}, {'content': \"## Gold stocks hitting $3,300/oz\\n\\nBack in early January, I saw a note saying gold might finally have its moment in 2025. At the time, gold was around $2,600 an ounce, and most people were still focused on tech and AI stocks. Honestly, I didn’t think much of it.\\n\\nNow it’s April, and gold just crossed $3,330. That’s a 27% jump in four months. And suddenly, fund managers are calling gold the most crowded trade on Wall Street beating out tech for the first time in two years.\\n\\nA lot happened, Inflation hasn’t cooled down as much as people hoped. There’s still a ton of uncertainty around interest rates. And global tensions whether it’s Ukraine, the Middle East, or China have pushed a lot of people toward what they see as “safer” investments.\\n\\nInstead of buying physical gold or a gold ETF, I started looking into gold mining companies. The basic idea is this: when gold prices go up, mining profits usually go up even faster. That’s because their costs stay fairly flat while the value of what they’re digging up increases. So you get more upside though more risk too.\\n\\nAccording to my research there are three types of gold stocks I’ve been looking at:\\n\\n1. Major producers — big mining companies with steady operations.\\n2. Junior miners — smaller companies that might hit it big with a new discovery, but come with more risk.\\n3. Streaming companies — these guys don’t mine gold themselves. They give money to miners upfront and get a portion of the gold in return. Less risk, but less reward too.\\n\\n  \\nHere's what I've been tracking:\\n\\nWheaton Precious Metals (WPM)  \\nThis one is a streaming company, so it doesn’t operate mines—it collects a share of gold from others. It’s a lower-risk model and still benefits from rising gold prices. Production is up, and they’re projecting long-term growth. The stock’s up over 50%.\\n\\nAgnico Eagle (AEM)  \\nSolid company with operations in Canada, Australia, and a few other places. They’ve been steadily growing reserves and investing in new exploration projects. Up nearly 90% this year.\\n\\nKinross Gold (KGC)  \\nIt’s up over 120% so far this year. They’ve been selling off riskier assets and doubling down on stable regions like the U.S., Canada, and Brazil. They’re also using AI and automation to cut costs. It’s starting to pay off.\\n\\nGold stocks have clearly run up. Valuations on some names especially the streaming companies are looking expensive. But at the same time, demand is still strong, and a lot of people see gold as a safe bet if things get worse economically or politically.\\n\\n  \\nWould it be safer to just buy a gold ETF instead? Something like GLD or IAU?\\n\\n\\n\\n  \\n\", 'subreddit': 'investing', 'score': 3.16210938, 'url': 'https://reddit.com/r/investing/comments/1k7e847', 'matched_query': 'semiconductor stocks recent performance'}, {'content': '## Warrants—USARW, CRMLW\\n\\nI am a newbie to investing and trading, and I’m very interested in materials mining companies, specifically rare earths. Today I was noticing USAR and CRML have warrant stocks (USARW and CRMLW, respectively).\\n\\nIf I want to buy USARW through my brokerage account, how do I figure out the conversion ratio to common stock? And how would I go about converting it in the future if the common stock goes up?', 'subreddit': 'pennystocks', 'score': 2.73632812, 'url': 'https://reddit.com/r/pennystocks/comments/1k7a03r', 'matched_query': 'semiconductor stocks recent performance'}]\u001b[0m\u001b[32;1m\u001b[1;3mBased on recent Reddit discussions using the query \"semiconductor stocks recent performance,\" there is very little direct conversation specifically about semiconductor stocks' recent movements. Most of the top posts are focused on other sectors (such as gold and mining stocks) or general market sentiment, with some discussion about volatility and retail investors buying dips.\n",
            "\n",
            "This suggests that while the broader market is experiencing volatility and sector rotation (with some investors moving from tech/semiconductors to perceived safer assets like gold), there hasn't been a major, focused discussion on semiconductor stocks' recent performance in the most active Reddit threads. The lack of direct posts may indicate that, at least in the last few days, semiconductor stocks have not been the primary topic of retail investor attention compared to other sectors.\n",
            "\n",
            "If you want a more detailed or company-specific update, let me know which semiconductor companies or tickers you're interested in!\u001b[0m\n",
            "\n",
            "\u001b[1m> Finished chain.\u001b[0m\n"
          ]
        },
        {
          "data": {
            "text/markdown": [
              "Based on recent Reddit discussions using the query \"semiconductor stocks recent performance,\" there is very little direct conversation specifically about semiconductor stocks' recent movements. Most of the top posts are focused on other sectors (such as gold and mining stocks) or general market sentiment, with some discussion about volatility and retail investors buying dips.\n",
              "\n",
              "This suggests that while the broader market is experiencing volatility and sector rotation (with some investors moving from tech/semiconductors to perceived safer assets like gold), there hasn't been a major, focused discussion on semiconductor stocks' recent performance in the most active Reddit threads. The lack of direct posts may indicate that, at least in the last few days, semiconductor stocks have not been the primary topic of retail investor attention compared to other sectors.\n",
              "\n",
              "If you want a more detailed or company-specific update, let me know which semiconductor companies or tickers you're interested in!"
            ],
            "text/plain": [
              "<IPython.core.display.Markdown object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "result = agent_executor.invoke({\"input\": query + \" just use one search query\"})\n",
        "\n",
        "display(Markdown(result['output']))"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "WM9Lz0eZXHQW",
      "metadata": {
        "id": "WM9Lz0eZXHQW"
      },
      "source": [
        "Once we're done, we can delete the index to save resources:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 31,
      "id": "12206f75-c8f6-474c-903f-582a5ec03709",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "12206f75-c8f6-474c-903f-582a5ec03709",
        "outputId": "97784c23-82cb-4010-904e-7c519225597d"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "{}"
            ]
          },
          "execution_count": 31,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "index.delete(delete_all=True)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0bLLVQWoYA5I",
      "metadata": {
        "id": "0bLLVQWoYA5I"
      },
      "source": [
        "---"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3 (ipykernel)",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
